import { reactive, ref } from 'vue';
import {
  loadData,
  start,
  parseControllers,
  type LoadData,
} from '../../lib/index';
import { message } from 'ant-design-vue';

export function useParse() {
  // 解析相关
  const apiMapRef = ref<any>({});
  const schemaMapRef = ref<any>({});

  // 控制器相关
  const controllersRef = ref<any[]>([]);
  const controllerNumberRef = ref<number>(0);

  function hanleParse(apiFoxJSON: LoadData | null) {
    if (!apiFoxJSON) {
      return;
    }
    controllersRef.value = [];

    if (!apiFoxJSON.apiCollection || !apiFoxJSON.schemaCollection) {
      return false;
    }

    loadData(apiFoxJSON);

    const { controllersNumber, controllers } = parseControllers();
    controllersRef.value = controllers;
    controllerNumberRef.value = controllersNumber;

    const { schemaMap, apiMap } = start();

    apiMapRef.value = apiMap;
    schemaMapRef.value = schemaMap;
  }

  return {
    apiMapRef,
    schemaMapRef,
    controllersRef,
    controllerNumberRef,
    hanleParse,
    parseControllers,
  };
}
